/**
 * 封装的request请求
 * @params 配置参数
 * @conType 请求类型
 */
import config from 'config.js';
class HTTP {
  constructor() {
    this.RestUrl = config.projectUrl
  }
  request(params, conType, showLoading = true) {
    var requestComplete = false;
    if(showLoading){
      wx.showLoading({
        title: '努力加载中...',
        icon: 'loading',
        mask: true
      })
      var nowDate = new Date().getTime()
      setTimeout(function() {
        if(requestComplete){
          wx.hideLoading({
            complete: (res) => {},
          })
        }  
      },config.limitLoadingTime)
    }
    var that = this;
    var url = that.RestUrl + params.url;
    var conType = conType;
    var authentication = wx.getStorageSync('authentication');
    if (!params.method) {
      params.method = 'GET';
    }
    if (!conType) {
      conType = 'application/x-www-form-urlencoded';
    } else if (conType == "postJson") {
      conType = 'application/json';
    }
    // 是否登录
    wx.request({
      url: url,
      data: params.data,
      method: params.method,
      header: {
        'content-type': conType,
        'authentication': authentication
      },
      success: function (res) {
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)

        var code = res.data.code;
        if (code == "402") {
          wx.showToast({
            title: '你还没有登录哦！',
            icon: 'none',
            image: '',
            duration: 1500,
            mask: false,
            success: (res) => {
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              }, 1500)
            }
          });
        } else {
          params.success && params.success(res.data);
        }
      },
      complete: function(res) {
        if(showLoading){
          requestComplete = true
          // console.log(res)
          var endDate = new Date().getTime()
          var time = endDate - nowDate
          if(time > config.limitLoadingTime){
            wx.hideLoading({
              complete: (res) => {

              },
            })
          }
        }
      },
      fail: function (err) {
        // wx.hideLoading();
        wx.showToast({
          title: '抱歉发生一个未知错误！请检查网络是否正常？',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    });
  }
  norequest(params, conType) {
    wx.showLoading({
      title: '努力加载中...',
      icon: 'loading',
      mask: true
    })

    var that = this;
    var url = that.RestUrl + params.url;
    var conType = conType;

    // console.log('url:', that.RestUrl);
    if (!params.method) {
      params.method = 'GET';
    }
    if (!conType) {
      conType = 'application/x-www-form-urlencoded';
    } else if (conType == "postJson") {
      conType = 'application/json';
    }

    wx.request({
      url: url,
      data: params.data,
      method: params.method,
      header: {
        'content-type': conType
      },
      success: function (res) {
        wx.hideLoading();
        var authentication = res.header.authentication
        if (authentication)
          wx.setStorageSync('authentication', authentication)
        
        params.success && params.success(res.data);
        
      },
      fail: function (err) {
        wx.hideLoading();
        wx.showToast({
          title: '抱歉发生一个未知错误！请检查网络是否正常？',
          icon: 'none',
          image: '',
          duration: 1500,
          mask: false
        });
      }
    });

  }
}
export {
  HTTP
}